const groupModels = require('../models/groupModel')
const {getHexByLength} = require("../utils/generateHex");
const getGroupInfo = (req, res) => {
    const groupId = req.params.groupId

    groupModels.getGroupInfoByGid(groupId).then(rs => {
        return res.status(200).send(rs).end()
    }).catch(err => {
        return res.status(500).send({msg: '获取群信息失败'}).end()
    })
}

const createGroup = (req, res) => {
    if (!req.file) return res.status(400).end()

    const fields = Object.values(req.body)
    fields.push(req.file.filename)
    fields.unshift(getHexByLength(8))
    fields.unshift(getHexByLength(8))

    groupModels.call_sp_create_group(fields)
        .then(rs => res.status(200).send(rs))
        .catch(err => res.status(500).send({msg:'创建失败，稍后再试'}))
}

const getGroupMember = (req, res) => {
    const groupId = req.params.groupId
    groupModels.getGroupMemberById(groupId)
        .then(rs => res.status(200).send(rs) )
        .catch(err => res.status(500).send({msg:'获取群成员失败，稍后再试'}))
}

const getChannel = (req, res) => {
    const groupId = req.params.groupId
    groupModels.getChannelByGid(groupId)
        .then(rs => res.status(200).send(rs) )
        .catch(err => res.status(500).send({msg:'获取频道失败，稍后再试'}))
}
const createChannel =  (req, res) => {
    const {group_id, channelName, icon, tag_id} = req.body
    console.log(tag_id, tag_id,tag_id,tag_id,tag_id,tag_id,tag_id)
    groupModels.createChannel([getHexByLength(14), group_id, tag_id, channelName, icon])
        .then(rs => res.status(200).send(rs) )
        .catch(err => res.status(500).send({msg:'创建频道失败，稍后再试', err}))
}

const updateGroup = (req, res) => {
    const {brief, group_name, gid} = req.body
    const {group_pic, banner} = req.files || {}

    const updateFields = []
    const updateValues = []

    if (brief) {
        updateFields.push('brief = ?')
        updateValues.push(brief)
    }

    if (group_name) {
        updateFields.push('group_name = ?')
        updateValues.push(group_name)
    }

    if (group_pic) {
        updateFields.push('group_pic = ?')
        updateValues.push(group_pic[0].filename)
    }

    if (banner) {
        updateFields.push('banner = ?')
        updateValues.push(banner[0].filename)
    } else if(req.body.banner === 'null') {
        updateFields.push('banner = ?')
        updateValues.push(null)
    }

    if (updateValues.length === 0) {
        return res.status(500).send({msg:'你特喵的改了什么你就保存？想干嘛？！'})
    }

    updateValues.push(gid)

    const sql = `UPDATE \`groups\` SET ${updateFields.join(', ')} WHERE id = ?`
    console.log(sql)
    console.table(updateValues)
    console.log(sql)

    groupModels.updateGroup(sql, updateValues)
        .then(rs => res.status(200).send(rs))
        .catch(err => res.status(500).send({msg:'修改失败，稍后再试', err}))
}

const getChannelTag = (req, res) => {
    const { groupId } = req.query
    groupModels.getChannelTag(groupId)
        .then(rs => res.status(200).send(rs) )
        .catch(err => res.status(500).send({msg:'创建频道失败，稍后再试', err}))
}

const createChannelTag = (req, res) => {
    const {groupId, tagName} = req.body
    groupModels.createChannelTag(groupId, tagName)
        .then(rs => res.status(200).send(rs))
        .catch(err => res.status(500).send({msg:'创建失败，稍后再试', err}))
}

    module.exports = {
    getGroupInfo,
    createGroup,
    updateGroup,
    getGroupMember,
    getChannel,
    getChannelTag,
    createChannel,
    createChannelTag
}

